package com.msmall.community.dpt.action.html;

import java.util.ArrayList;
import java.util.List;

import com.msmall.base.action.BaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.dpt.service.SegmentManager;
import com.msmall.community.dpt.vo.SegmentVO;

public class SegmentAction extends BaseAction{

	private static final long serialVersionUID = -3483657891065423350L;
	
	/**
	 * q:提问分类 wiki百科分类 klg知识 cookbook食谱
	 */
	private String module;
	
	/**
	 * 1：第一级别
	 * 2：第二级别
	 * 3: 第三级别
	 */
	private int level;
	
	private Long id;
	
	private List<SegmentVO> segments;
	
	private SegmentManager segmentManager;

	/**
	 * 获得分类。 Ajax.json使用
	 */
	@SuppressWarnings("unchecked")
	public String get(){
		try{
			segments = new ArrayList<SegmentVO>();
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			List<CascadeCollection<Segment>> children = null;
			if("q".equals(module)){
				if(1 == level){
					CascadeCollection<Segment> navSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_QUESTION_NAV);
					children = navSegments.getChildren();
				}else if(2 == level){
					CascadeCollection<Segment> parent = cs.getAllDatas().get(id);
					children = parent.getChildren();
				}
				for(int i=0;i<children.size(); i++){
					CascadeCollection<Segment> current = children.get(i);
					SegmentVO vo = new SegmentVO();
					vo.setValue(String.valueOf(current.getCurrent().getSegmentid()));
					vo.setText(current.getCurrent().getTitle());
					segments.add(vo);
				}
			}else if("wiki".equals(module)){
				if(1 == level){
					CascadeCollection<Segment> navSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_WIKI_NAV);
					children = navSegments.getChildren();
				}else if(2 == level){
					CascadeCollection<Segment> parent = cs.getAllDatas().get(id);
					children = parent.getChildren();
				}else if(3 == level){
					CascadeCollection<Segment> parent = cs.getAllDatas().get(id);
					children = parent.getChildren();
				}
				for(int i=0;i<children.size(); i++){
					CascadeCollection<Segment> current = children.get(i);
					SegmentVO vo = new SegmentVO();
					vo.setValue(String.valueOf(current.getCurrent().getSegmentid()));
					vo.setText(current.getCurrent().getTitle());
					segments.add(vo);
				}
			}else if("klg".equals(module)){
				if(1 == level){
					CascadeCollection<Segment> navSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_NAV);
					children = navSegments.getChildren();
				}else if(2 == level){
					CascadeCollection<Segment> parent = cs.getAllDatas().get(id);
					children = parent.getChildren();
				}
				for(int i=0;i<children.size(); i++){
					CascadeCollection<Segment> current = children.get(i);
					SegmentVO vo = new SegmentVO();
					vo.setValue(String.valueOf(current.getCurrent().getSegmentid()));
					vo.setText(current.getCurrent().getTitle());
					segments.add(vo);
				}
			}else if("cookbook".equals(module)){
				if(1 == level){
					CascadeCollection<Segment> navSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_COOKBOOK_NAV);
					children = navSegments.getChildren();
				}else if(2 == level){
					CascadeCollection<Segment> parent = cs.getAllDatas().get(id);
					children = parent.getChildren();
				}
				for(int i=0;i<children.size(); i++){
					CascadeCollection<Segment> current = children.get(i);
					SegmentVO vo = new SegmentVO();
					vo.setValue(String.valueOf(current.getCurrent().getSegmentid()));
					vo.setText(current.getCurrent().getTitle());
					segments.add(vo);
				}
			}
			
		}catch(Exception e){
			log.debug(e);
		}
		return SUCCESS;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public List<SegmentVO> getSegments() {
		return segments;
	}

	public void setSegments(List<SegmentVO> segments) {
		this.segments = segments;
	}

	public void setSegmentManager(SegmentManager segmentManager) {
		this.segmentManager = segmentManager;
	}
}
